package com.taikang.controller;

import com.taikang.entity.Employee;
import com.taikang.entity.SpecialServiceOrder;
import com.taikang.service.EmployeeService;
import com.taikang.service.GrabService;
import com.taikang.util.ResponseData;
import com.taikang.util.Status;
import com.taikang.util.page.Page;
import com.taikang.util.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zhai on 16/5/10.
 */
@Controller
public class GrabOrderController {

    private GrabService grabService;
    private EmployeeService employeeService;

    @Autowired
    public void setGrabService(GrabService grabService) {
        this.grabService = grabService;
    }

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @RequestMapping(value = "/grab/list")
    @ResponseBody
    public Object getGrabList(@RequestParam("employeeId") Integer employeeId,
                              @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "size", required = false, defaultValue = "3") Integer size) {
        ResponseData responseData = new ResponseData();
        if (null == employeeId || employeeId <= 0) {
            responseData.setStatus(Status.error);
            responseData.setMsg("无法获取用户信息,请重新登录");
            return responseData;
        } else {
            Page grabPage = new Page(page, size);
            Employee employee = employeeService.getEmployeeById(employeeId);
            if(employee.getStatusID() == 2) {
                responseData.setStatus(Status.error);
                responseData.setMsg("员工已离职,无法抢单");
                return responseData;
            }
            Pagination data = grabService.getGrabListByAgent(employee.getAgentID(), grabPage);
            if (data.getCount() == 0) {
                responseData.setStatus(Status.error);
                responseData.setMsg("暂无数据");
            } else {
                responseData.setStatus(Status.success);
                responseData.setData(data);
            }
        }
        return responseData;
    }

    @RequestMapping(value = "/grab/confirm")
    @ResponseBody
    public Object confirmGrab(@RequestParam("employeeId") Integer employeeId, @RequestParam("orderId") Integer orderId) {
        ResponseData responseData = new ResponseData();
        if (null == employeeId || null == orderId) {
            responseData.setStatus(Status.error);
            responseData.setMsg("无法获取用户信息,请重新登录");
        } else {
            SpecialServiceOrder order = grabService.getById(orderId);
            if (null == order) {
                responseData.setStatus(Status.error);
                responseData.setMsg("订单已失效,抢单失败");
            }
            if (order.getOrderStatus() != 1) {
                responseData.setStatus(Status.error);
                responseData.setMsg("订单已经被抢或已取消");
            } else {
                order.setEmployeeID(String.valueOf(employeeId));
                order.setOrderStatus(2);
                if (grabService.update(order) > 0) {
                    responseData.setStatus(Status.success);
                    responseData.setMsg("抢单成功,请前往我的任务查看");
                } else {
                    responseData.setStatus(Status.error);
                    responseData.setMsg("抢单失败,请稍后再试");
                }
            }
        }
        return responseData;
    }
}
